<?php
// +----------------------------------------------------------------------
// | Yzncms [ 御宅男工作室 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://yzncms.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 御宅男 <530765310@qq.com>
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 接口调用日志
// +----------------------------------------------------------------------
namespace app\api\controller;
use think\Controller;
use think\Request;
use think\Db;

/*
 	接口调用返参举例
 * 
 * 失败
 $common = new Common($ticketId,$ticketMd5,__METHOD__,"失败");
 return $common->dataFormat('','json',__FUNCTION__,$code = 0,$message = '缺少参数');
 * 
 * 成功
 $common = new Common($marketId,$key,__METHOD__,"成功");
 return $common->dataFormat('','json',__FUNCTION__);
 * 
 * 
 * */
class Common extends Controller
{
	/*
	 * crm接口
	 * 商场appid -> $market_appid
	 * 商场密钥 -> $market_secretKey
	 * 接口名称-> $api_name
	 * 接口调用返回信息 $message
	 */
    /*
     *构造方法，接受传参  
     * */
	public function __construct()
	{
		parent::__construct();
	}
   	/*
     *接口统一返回数据格式
     * $type  1:'json'; 2:'xml'
     * $functionName 接口方法名
     * $message 返回信息 string
     * */
   	public function dataFormat($inParam,$outParam,$functionName,$message,$dataType="json",$status = "成功")
   	{
// 		$request = new Request;;
//      $post     = $request->query();
		//$dataType 入参数据类型
		if($dataType == 'json'){
			$logData = [
		    	'ip' => get_client_ip(),
		    	'createtime' => date('Y-m-d H:i:s'),
		    	'api' => $functionName,
		    	'message' => $message,
		    	'inParam' => json_encode($inParam),
		    	'outParam' => json_encode($outParam),
		    	'status' => $status
		    ];
		}else{
			$logData = [
		    	'ip' => get_client_ip(),
		    	'createtime' => date('Y-m-d H:i:s'),
		    	'api' => $functionName,
		    	'message' => $message,
		    	'inParam' => $inParam,
		    	'outParam' => json_encode($outParam),
		    	'status' => $status
		    ];
		}
			
	    $apilogId = Db::name('apilog')
			->insertGetId($logData);
   		return $apilogId;
   	}
   	
}
